package demo;

import com.alibaba.fastjson.JSON;
import demo.business.TestImpl;
import demo.business.TestInterface;
import demo.business.User;
import jk.fox.config.RpcConfiguration;
import jk.fox.engine.DefaultRpcEngine;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @author cuichao
 * @Description: RpcTest
 * @Date: create in 2020/6/1 1:08
 */
public class RpcTest {

    public static void main(String[] args) {
        RpcConfiguration.RpcConfigBuilder builder = RpcConfiguration.builder();
        builder
                .expose(7777)
                .enableSameSource(false);
        /*transport builder*/
        builder
                .netty()
                .clientHeartBeat(10)
                .serverHeartBeat(10)
                .serverTcpBacklog(25512)
                .serverWorkGroup(0)
                .keepAlive(true)
                .and()
                /*registry builder*/
                .zookeeper()
                .address("127.0.0.1:2181")
                .maxRetires(3)
                .retryIntervalMs(1000)
                .connectionTimeoutMs(15000)
                .sessionTimeoutMs(5000);

        RpcConfiguration config = builder.build();


        DefaultRpcEngine engine = new DefaultRpcEngine(config,null);
        engine.registry(TestInterface.class,new TestImpl());
        engine.registry(TestInterface.class);



        try {
            engine.start();
        } catch (Exception e) {
            e.printStackTrace();
        }


        String param = "test1";
        User user = new User(32,"cuichao");
        int flag = 22;
        List<String> strings = new ArrayList<>( Arrays.asList("1", "2", "3"));
        List<User> users  = new ArrayList<>( Arrays.asList(user));

        TestInterface instance = (TestInterface)engine.getConsumer(TestInterface.class.getName());



        try {
            Thread.sleep(3000);
            System.out.println("first");
            Map<String, Object> test = instance.test(param, user, users, flag);
            System.out.println("result is :"+JSON.toJSONString(test));
            Thread.sleep(10000);
        }catch (Exception e){
            System.out.println("调用端: "+e);
            e.printStackTrace();
        }
        try {
            System.out.println("second");
            Map<String, Object> test1 = instance.test(param, user, users, flag);
            System.out.println("result is :"+JSON.toJSONString(test1));
            Thread.sleep(1000);
        }catch (Exception e){
            System.out.println("调用端: "+e);
            e.printStackTrace();
        }
        try {

            System.out.println("third");
            Map<String, Object> test2= instance.test(param, user, users, flag);
            System.out.println("result is :"+JSON.toJSONString(test2));
            Thread.sleep(2000);
        }catch (Exception e){
            System.out.println("调用端: "+e);
            e.printStackTrace();
        }


    }


}
